package com.gitee.wsl.struct.registry

import com.gitee.wsl.logger.Logger
import com.gitee.wsl.struct.register.RegistryException
//import com.gitee.wsl.struct.register.SimpleRegistry
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.jvmErasure


/**
 * Implements simple constructor injection that can easily replace your more complex dependency injection framework.
 * You may extend this class to override how exactly constructor parameters are created.
 */
//open class DependencyInjectingRegistry: SimpleRegistry() {
//
//    override fun <T: Any> create(type: KClass<T>): T {
//        val constructor = chooseConstructor(type) ?: throw RegistryException("$type has no usable constructor")
//        try {
//            val args = createArgs(constructor)
//            return constructor.callBy(args).also {
//                Logger.d { "Auto-created ${type.simpleName}${args.values.map { it::class.simpleName }}" }
//            }
//        } catch (e: Throwable) {
//            throw RegistryException("Failed to auto-create ${type.simpleName} with dependencies on ${constructor.parameters.map {it.type}}", e.cause ?: e)
//        }
//    }
//
//    protected open fun <T : Any> chooseConstructor(type: KClass<T>): KFunction<T>? =
//        type.primaryConstructor ?: type.constructors.minByOrNull { it.parameters.size }
//
//    protected open fun <T : Any> createArgs(constructor: KFunction<T>): Map<KParameter, Any> =
//        constructor.parameters.filter { !it.isOptional || contains(it.type.jvmErasure) }.associateWith { require(it.type.jvmErasure) }
//}
